package com.gmall.rabbitmq.consumer.service;

import com.gmall.rabbitmq.config.DirectExchange_Queue_Config;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service

public class DirectExQueueService {

    @RabbitListener(queues = DirectExchange_Queue_Config.DIRECT_EX_QUEUE_1)
    public void consumeSmsMsg(Channel channal, Message message, String msg){
        try {
            long deliveryTag = message.getMessageProperties().getDeliveryTag();
            String consumerQueue =
                    message.getMessageProperties().getConsumerQueue();
            System.out.println(consumerQueue +":" +msg);

            channal.basicAck(deliveryTag,false);
            System.out.println("手动发送ACK");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @RabbitListener(queues = DirectExchange_Queue_Config.DIRECT_EX_QUEUE_2)
    public void consumeEmailMsg(Channel channal, Message message, String msg){
        try {
            long deliveryTag = message.getMessageProperties().getDeliveryTag();
            String consumerQueue =
                    message.getMessageProperties().getConsumerQueue();
            System.out.println(consumerQueue +":" +msg);

            int i = 10/0;//模拟业务出现异常
            channal.basicAck(deliveryTag,false);//手动发送ACK
            System.out.println("手动发送ACK");
        } catch (Exception e) {
            System.out.println("业务异常，拒绝消息，不发送ACK");
            throw new RuntimeException(e);
        }
    }
    @RabbitListener(queues = DirectExchange_Queue_Config.DIRECT_EX_QUEUE_3)
    public void consumeQQMsg(Channel channal, Message message, String msg){
        try {
            long deliveryTag = message.getMessageProperties().getDeliveryTag();
            String consumerQueue = message.getMessageProperties().getConsumerQueue();
            System.out.println(consumerQueue +":" +msg);

            channal.basicAck(deliveryTag,false);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
